package com.xialinrui.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xialinrui.dto.AffairDto;
import com.xialinrui.dto.DailyMoneyDto;
import com.xialinrui.dto.MonthlyMoneyDto;
import com.xialinrui.dto.TodoDto;
import com.xialinrui.entity.Affair;
import com.xialinrui.entity.MoodRecord;
import com.xialinrui.entity.Replay;
import com.xialinrui.entity.ResponseResult;
import com.xialinrui.service.AffairService;
import com.xialinrui.service.ReplayService;
import com.xialinrui.vo.ExpenditureCategoryVo;
import com.xialinrui.vo.IncomeCategoryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xialinrui
 */
@RestController
@Slf4j
@RequestMapping("replay")
public class ReplayController {
    @Resource
    private ReplayService replayService;
    @Resource
    private AffairService affairService;

    @PostMapping("/add")
    public ResponseResult<Map<String, Integer>> add(@RequestBody Replay replay) {
        replayService.addOneInstant(replay);
        HashMap<String, Long> resMap = new HashMap<>(1);
        resMap.put("id", replay.getId());
        return new ResponseResult(200, "创建成功", resMap);
    }

    @PutMapping("/update")
    public ResponseResult update(@RequestBody Replay replay) {
        replayService.updateById(replay);
        return new ResponseResult(200, "保存成功");
    }

    @DeleteMapping("/delete")
    public ResponseResult delete(@RequestBody List<Long> ids) {
        replayService.delete(ids);
        return new ResponseResult(200, "删除成功");
    }

    /**
     * 查询所有复盘记录
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public ResponseResult<Page<Replay>> getAll(@RequestParam Integer page, @RequestParam Integer pageSize) {
        Page<Replay> resPage = new Page<>(page, pageSize);
        QueryWrapper<Replay> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("update_time").orderByDesc("create_time");
        replayService.page(resPage, queryWrapper);
        return new ResponseResult(200, "查询成功", resPage);
    }

    /**
     * 根据输入框模糊查询复盘记录
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("list/like")
    public ResponseResult<Page<Replay>> getLike(@RequestParam Integer page, @RequestParam Integer pageSize, @RequestParam String content) {
        Page<Replay> resPage = new Page<>(page, pageSize);
        QueryWrapper<Replay> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", content).orderByDesc("update_time").orderByDesc("create_time");
        replayService.page(resPage, queryWrapper);
        return new ResponseResult(200, "查询成功", resPage);
    }

    /**
     * 根据id显示文章详情
     *
     * @param id
     * @return
     */
    @GetMapping("/show")
    public ResponseResult<MoodRecord> show(@RequestParam Long id) {
        return new ResponseResult(200, "显示成功", replayService.getById(id));
    }

    /**
     * 根据时间筛选文章
     *
     * @param from
     * @param to
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/list/date")
    public ResponseResult<Page<Replay>> getByDate(@RequestParam Date from, @RequestParam Date to, @RequestParam Integer page, @RequestParam Integer pageSize) {
        Page<Replay> resPage = new Page<>(page, pageSize);
        QueryWrapper<Replay> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("create_time", from, to).orderByDesc("update_time").orderByDesc("create_time");
        replayService.page(resPage, queryWrapper);
        return new ResponseResult(200, "查询成功", resPage);
    }

    /**
     * 显示一天的事务和任务们等等
     *
     * @param date
     * @return
     */
    @GetMapping("/everyday")
    public ResponseResult<List<AffairDto>> getEverydayTable(@RequestParam Date date) {
        List<AffairDto> resList = new ArrayList<>();
        QueryWrapper<Affair> affairQueryWrapper = new QueryWrapper<>();
        affairQueryWrapper.eq("date", date).orderByAsc("start_time").orderByAsc("end_time");
        List<Affair> affairs = affairService.list(affairQueryWrapper);
        for (Affair affair : affairs) {
            Long id = affair.getId();
            AffairDto affairDto = new AffairDto();
            affairDto.setId(affair.getId());
            affairDto.setDate(affair.getDate());
            affairDto.setStartTime(affair.getStartTime());
            affairDto.setEndTime(affair.getEndTime());
            affairDto.setRealTime(affair.getRealTime());
            affairDto.setWeekNumber(affair.getWeekNumber());
            affairDto.setRemarks(affair.getRemarks());
            List<TodoDto> todoDtos = affairService.selectAllTodosByAffairId(id);
            ArrayList<TodoDto> resTodos = new ArrayList<>();
            for (TodoDto todoDto : todoDtos) {
                if(todoDto.getId() != null) {
                    resTodos.add(todoDto);
                }
            }
            affairDto.setTodos(resTodos);
            resList.add(affairDto);
        }
        return new ResponseResult<>(200, "查询成功", resList);
    }

    /**
     * 根据起始日期和终止日期查询每日收入和支出
     *
     * @param from
     * @param to
     * @return
     */
    @GetMapping("/money/daily")
    public ResponseResult<Map<String, List<Object>>> getMoneyDaily(@RequestParam String from, @RequestParam String to) {
        List<DailyMoneyDto> incomeList = replayService.moneyDailyIncome(from, to);
        List<DailyMoneyDto> expenditureList = replayService.moneyDailyExpenditure(from, to);
        Map<String, List<Object>> resMap = new HashMap<>(3);
        resMap.put("日期", new ArrayList<>());
        resMap.put("收入", new ArrayList<>());
        resMap.put("支出", new ArrayList<>());
        for (int i = 0; i < incomeList.size(); i++) {
            String day = incomeList.get(i).getDay();
            Double income = incomeList.get(i).getIncome();
            Double expend = expenditureList.get(i).getExpenditure();
            resMap.get("日期").add(day);
            resMap.get("收入").add(income);
            resMap.get("支出").add(expend);
        }
        return new ResponseResult(200, "查询成功", resMap);
    }


    /**
     * 根据起始月份和终止月份查询每月收入和支出
     *
     * @param from
     * @param to
     * @return
     */
    @GetMapping("/money/monthly")
    public ResponseResult<Map<String, List<Object>>> getMoneyMonthly(@RequestParam String from, @RequestParam String to) {
        List<MonthlyMoneyDto> incomeList = replayService.moneyMonthlyIncome(from, to);
        List<MonthlyMoneyDto> expenditureList = replayService.moneyMonthlyExpenditure(from, to);
        Map<String, List<Object>> resMap = new HashMap<>(3);
        resMap.put("月份", new ArrayList<>());
        resMap.put("收入", new ArrayList<>());
        resMap.put("支出", new ArrayList<>());
        for (int i = 0; i < incomeList.size(); i++) {
            String day = incomeList.get(i).getMonth();
            Double income = incomeList.get(i).getIncome();
            Double expend = expenditureList.get(i).getExpenditure();
            resMap.get("月份").add(day);
            resMap.get("收入").add(income);
            resMap.get("支出").add(expend);
        }
        return new ResponseResult(200, "查询成功", resMap);
    }

    /**
     * 根据起始年份和终止年份查询每年收入和支出
     *
     * @param from
     * @param to
     * @return
     */
    @GetMapping("/money/yearly")
    public ResponseResult<Map<String, List<Object>>> getMoneyYearly(@RequestParam String from, @RequestParam String to) {
        List<MonthlyMoneyDto> incomeList = replayService.moneyYearlyIncome(from, to);
        List<MonthlyMoneyDto> expenditureList = replayService.moneyYearlyExpenditure(from, to);
        Map<String, List<Object>> resMap = new HashMap<>(3);
        resMap.put("年份", new ArrayList<>());
        resMap.put("收入", new ArrayList<>());
        resMap.put("支出", new ArrayList<>());
        for (int i = 0; i < incomeList.size(); i++) {
            String day = incomeList.get(i).getMonth();
            Double income = incomeList.get(i).getIncome();
            Double expend = expenditureList.get(i).getExpenditure();
            resMap.get("年份").add(day);
            resMap.get("收入").add(income);
            resMap.get("支出").add(expend);
        }
        return new ResponseResult(200, "查询成功", resMap);
    }


    /**
     * 根据起始时间和终止时间与类别显示收入饼图数据
     *
     * @param from
     * @param to
     * @return
     */
    @GetMapping("/income/category")
    public ResponseResult<List<IncomeCategoryVo>> getIncomeMoneyByCategory(@RequestParam String from, @RequestParam String to) {
        List<IncomeCategoryVo> resList = replayService.getIncomeMoneyByCategory(from, to);
        return new ResponseResult<>(200, "查询成功", resList);
    }


    /**
     * 根据起始时间和终止时间与类别显示支出饼图数据
     *
     * @param from
     * @param to
     * @return
     */
    @GetMapping("/expenditure/category")
    public ResponseResult<List<ExpenditureCategoryVo>> getExpenditureMoneyByCategory(@RequestParam String from, @RequestParam String to) {
        List<ExpenditureCategoryVo> resList = replayService.getExpenditureMoneyByCategory(from, to);
        return new ResponseResult<>(200, "查询成功", resList);
    }


}
